﻿using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using TciNewsReader_2014_04.Interfaces;
namespace TciNewsReader_2014_04.Infrastructure
{
    public class Logger : ILogger
    {
        private static readonly Semaphore Semaphore = new Semaphore(1,1);
        private int _messageCount;
        public Logger()
        {
            File.WriteAllText(Constants.LogFileLocation, String.Empty);
        }
        public void Log(string message)
        {
            try
            {
                Semaphore.WaitOne();
                string fullMessage = String.Format("{0}: {1} {2}", DateTime.Now.ToString("s"), message, Environment.NewLine);
                File.AppendAllText(Constants.LogFileLocation, fullMessage);
                ++_messageCount;
            }
            catch (Exception ex)
            {
                Console.Beep();
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Semaphore.Release();
            }
        }
        public bool HasContent()
        {
            return _messageCount > 0;
        }
        public void Show()
        {
            Process.Start(Constants.LogFileLocation);
        }
    }
}
